package com.technology.education

import android.content.Context
import android.util.Log
import com.technology.education.data.AppData
import com.technology.education.ext.showLog
import com.tencent.android.tpush.XGIOperateCallback
import com.tencent.android.tpush.XGPushConfig
import com.tencent.android.tpush.XGPushManager
import com.tencent.bugly.Bugly
import com.tencent.bugly.beta.Beta
import com.tencent.mmkv.MMKV
import me.hgj.jetpackmvvm.base.BaseApp
import kotlin.properties.Delegates


/**
 *@author:create by BZY
 *Date: 2022/2/24 10
 */
class MyApplication : BaseApp() {

    companion object {
        private var app: MyApplication? = null
        var context: Context by Delegates.notNull()
        fun getApp(): MyApplication? {
            return app
        }
    }

    override fun onCreate() {
        super.onCreate()
        app = this
        context = applicationContext
        MMKV.initialize(this)
        initXG()
        initBugly()

    }

    private fun initBugly() {
        Beta.autoInit = true
        Beta.enableNotification = true
        Beta.enableHotfix = false
        Beta.autoCheckAppUpgrade = true
        Beta.smallIconId = R.mipmap.logo
        Beta.largeIconId = R.mipmap.logo
        Bugly.init(this, "6110955707", BuildConfig.DEBUG)
    }

    private fun initXG() {
        XGPushConfig.enableDebug(this, BuildConfig.DEBUG)
        XGPushConfig.Build(context).setLogLevel(Log.ERROR)
        XGPushManager.registerPush(this, object : XGIOperateCallback {
            override fun onSuccess(data: Any, flag: Int) {
                AppData.PUSH_TOKEN = data.toString()
                //token在设备卸载重装的时候有可能会变
                showLog("注册成功，设备token为：$data")
            }

            override fun onFail(data: Any, errCode: Int, msg: String) {
                showLog("注册失败，错误码：$errCode,错误信息：$msg")
            }
        })
    }
}